 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-12-15
 */
package com.ce.pms.custservice.service.impl;

import java.util.List;
import java.util.Set;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.custservice.dao.ISubmitTaskDao;
import com.ce.pms.custservice.model.CustServiceEventDO;
import com.ce.pms.custservice.model.SubmitTaskDO;
import com.ce.pms.custservice.service.ICustEventStateMachineService;
import com.ce.pms.custservice.vo.CustServiceEventVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.IReturnVisitRuleDao;
import com.ce.pms.system.custservice.dao.ISignReadingRuleDao;
import com.ce.pms.system.custservice.model.ReturnVisitRuleDO;
import com.ce.pms.system.custservice.model.SignReadingRuleDO;

/**
 * @author yandawei
 *
 */
public class CustEventStateMachineServiceImpl extends PmsBaseServiceImpl implements
		ICustEventStateMachineService {

	private ISignReadingRuleDao signReadingRuleDao ;
	private IReturnVisitRuleDao returnVisitRuleDao ;
	private ISubmitTaskDao submitTaskDao;

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#decideALLStatuzWithEditCSEvent(com.ce.pms.custservice.vo.CustServiceEventVO, com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public void decideALLStatuzWithEditCSEvent(CustServiceEventVO csEventVO,
			CustServiceEventDO csEventDO) {
		super.checkParameterIsNull(csEventDO, "客户事件不允许为空");
		super.checkParameterIsNull(csEventVO, "客户事件不允许为空");
		boolean isNewStatus = CustServiceEventDO.EVENT_STATUS_NEW_CODE.equals(csEventDO.getEventStatuz());
		boolean isArchivingStatus =CustServiceEventDO.EVENT_STATUS_ARCHIVING_CODE.equals(csEventDO.getEventStatuz());
		boolean isVOValid = CustServiceEventDO.EVENT_INVALID_CODE.equals(csEventVO.getValidity());
		boolean isOKValid = CustServiceEventDO.EVENT_VALID_CODE.equals(csEventVO.getValidity());
		boolean isWAITValid = CustServiceEventDO.EVENT_WAITVALID_CODE.equals(csEventVO.getValidity());
		if(isNewStatus) {
			if(isVOValid) {
				csEventDO.setEStatuzWaitingArchivism();
			}
		}
		else if(isArchivingStatus ){
			if(csEventDO.getValidity().equals(CustServiceEventDO.EVENT_VALID_CODE)){
				throw new BusinessException("此客户事件不允许修改");
			}
			else if(isOKValid || isWAITValid){
				csEventDO.setEStatuzNew();
			}
		}else{
			throw new BusinessException("此客户事件不允许修改");
		}


	}

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#decideAllStatuzWithAddCSEvent(com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public void decideAllStatuzWithAddCSEvent(CustServiceEventDO csEvent) {
		super.checkParameterIsNull(csEvent, "客户事件不允许为空");

		boolean isValid = CustServiceEventDO.EVENT_VALID_CODE.equals(csEvent.getValidity());
		boolean isWaitValid = CustServiceEventDO.EVENT_WAITVALID_CODE.equals(csEvent.getValidity());
		boolean isInValid = CustServiceEventDO.EVENT_INVALID_CODE.equals(csEvent.getValidity());
		if(isValid || isWaitValid){
			csEvent.setEventStatuz(CustServiceEventDO.EVENT_STATUS_NEW_CODE);
		}else if(isInValid){
			csEvent.setEStatuzWaitingArchivism();
		}

	}

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#decideAllStatuzWithAddReturnVisit(com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public void decideAllStatuzWithAddReturnVisit(CustServiceEventDO csEvent) {
			super.checkParameterIsNull(csEvent, "客户事件不允许为空");

			boolean isCompletedStatus = CustServiceEventDO.EVENT_STATUS_COMPLETE_CODE.equals(csEvent.getEventStatuz());
			boolean isArchivingStatus = CustServiceEventDO.EVENT_STATUS_ARCHIVING_CODE.equals(csEvent.getEventStatuz());
			if(isCompletedStatus || isArchivingStatus){
					csEvent.setRVStatuzHasReturnVisited();
			}else{
					throw new BusinessException("目前不能做回访");
		    }

			boolean isMustRevier = remindSignReading(csEvent);
			if(isMustRevier && isCompletedStatus && csEvent.getReviewerStatuz() == null){
				csEvent.setRStatuzWaitingSignRead();
			}
			if(isCompletedStatus && csEvent.getReviewerStatuz() == null) {
				csEvent.setEStatuzWaitingArchivism();
			}
		}



	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#decideAllStatuzWithAddSignReading(com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public void decideAllStatuzWithAddSignReading(CustServiceEventDO csEvent) {
		super.checkParameterIsNull(csEvent, "客户事件不允许为空");

		boolean isCompletedStatus = CustServiceEventDO.EVENT_STATUS_COMPLETE_CODE.equals(csEvent.getEventStatuz());
		boolean isArchivingStatus = CustServiceEventDO.EVENT_STATUS_ARCHIVING_CODE.equals(csEvent.getEventStatuz());
		boolean isCompleteReview = CustServiceEventDO.REVIEWER_STATUZ_COMPLETE_CODE.equals(csEvent.getReviewerStatuz());

		if((isCompletedStatus || isArchivingStatus )&& !isCompleteReview ){
			csEvent.setRStatuzHasSignReaded();
		}else{
			throw new BusinessException("当前不能阅签");
		}

		if(isCompletedStatus){
			csEvent.setEStatuzWaitingArchivism();
		}
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#decideAllStatuzWithComplete(com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public void decideAllStatuzWithComplete(CustServiceEventDO csEvent) {
		super.checkParameterIsNull(csEvent, "客户事件不允许为空");

		List list = submitTaskDao.findAllResult("from SubmitTaskDO task join task.custServiceEventDO event " +
				"where task.validity=1 and task.statuz !='003' and event.keyword=?", new Long[]{csEvent.getKeyword()});

		if(list != null && list.size()>0){
			throw new BusinessException("先把提交任务处理完");
		}
		csEvent.setEStatuzComplete();
		if(!(remindReturnVisit(csEvent) || remindSignReading(csEvent))){
			csEvent.setEStatuzWaitingArchivism();
		}
		boolean isCompletedStatus = CustServiceEventDO.EVENT_STATUS_COMPLETE_CODE.equals(csEvent.getEventStatuz());
		if(isCompletedStatus){
			if(remindReturnVisit(csEvent)){
				csEvent.setRVStatuzWaitingReturnVisit();
			}else if(csEvent.getReturnVisitStatuz() == null &&remindSignReading(csEvent)){
				csEvent.setRStatuzWaitingSignRead();
			}

		}


	}

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#remindReturnVisit(com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public boolean remindReturnVisit(CustServiceEventDO csEvent) {
		super.checkParameterIsNull(csEvent, "客户事件不允许为空");
		Long id = csEvent.getCustomerServiceEventKindDO().getKeyword();
		Long id1 = null;
		if(csEvent.getEmergencyDegreeDO()!=null) {
			id1=csEvent.getEmergencyDegreeDO().getKeyword();
		}
		List<ReturnVisitRuleDO> rules = returnVisitRuleDao.queryAllValidReturnVisitRule();
		boolean needReturn = false;
		for(ReturnVisitRuleDO rule : rules) {
			Long tempId = null;
			Long EventKind = null;
			if(null!=rule.getEmergencyDegree()) {
				tempId=rule.getEmergencyDegree().getKeyword();
			}
			if(null!=rule.getCustServEventKind()){
				EventKind = rule.getCustServEventKind().getKeyword();
			}

			if((EventKind!=null && EventKind.equals(id))
					&& (tempId!=null &&tempId.equals(id1))) {
				needReturn = true;
				break;
			}
		}
		return needReturn;
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.model.ICustEventStateMachineService#remindSignReading(com.ce.pms.custservice.model.CustServiceEventDO)
	 */
	public boolean remindSignReading(CustServiceEventDO csEvent) {
		super.checkParameterIsNull(csEvent, "客户事件不允许为空");
		Long id = csEvent.getCustomerServiceEventKindDO().getKeyword();
		Long id1 = null;
		if(csEvent.getEmergencyDegreeDO()!=null) {
			id1=csEvent.getEmergencyDegreeDO().getKeyword();
		}
		List<SignReadingRuleDO> rules = signReadingRuleDao.queryAllValidSignReadingRule();
		Boolean needSign = false;
		for(SignReadingRuleDO rule : rules) {
//			if(rule.getKeyword().equals(id)) {
			Long tempId = null;
			Long EventKind = null;
			if(null!=rule.getEmergencyDegree()) {
				tempId=rule.getEmergencyDegree().getKeyword();
			}
			if(null!=rule.getCustServEventKind()){
				EventKind = rule.getCustServEventKind().getKeyword();
			}
			if((EventKind!=null && EventKind.equals(id))
					&& (tempId!=null &&tempId.equals(id1))) {
				needSign = true;
				break;
			}
		}

		return needSign;
	}



	public void setReturnVisitRuleDao(IReturnVisitRuleDao returnVisitRuleDao) {
		this.returnVisitRuleDao = returnVisitRuleDao;
	}

	public void setSignReadingRuleDao(ISignReadingRuleDao signReadingRuleDao) {
		this.signReadingRuleDao = signReadingRuleDao;
	}

	public void setSubmitTaskDao(ISubmitTaskDao submitTaskDao) {
		this.submitTaskDao = submitTaskDao;
	}

	public Boolean isAllowManualTriggerComplete(CustServiceEventDO eventDO)
	{
		//1 事件的状态只有 "新建"或"处理中（已提交）"，才能完工
		String eventStatus = eventDO.getEventStatuz();
		if(eventStatus.equals(CustServiceEventDO.EVENT_STATUS_NEW_CODE) || eventStatus.equals(CustServiceEventDO.EVENT_STATUS_SUBMIT_CODE))
		{
			//2 事件关联的 任务 存在状态为 "处理中" 时，不能完工，
			List<SubmitTaskDO> taskDOList = submitTaskDao.querySubmitTaskListByEventID(eventDO.getKeyword(),null);
			if(!BlankUtil.isBlank(taskDOList))
			{
				for(SubmitTaskDO submitTaskDO : taskDOList)
				{
					if(submitTaskDO.getStatuz().equals(SubmitTaskDO.TASK_ACCEPTED_HANDLE_CODE)
							|| submitTaskDO.getStatuz().equals(SubmitTaskDO.TASK_PREPARE_HANDLE_CODE))
					{
						throw new BusinessException("custservice:custService_event_complete_not_allow_exception",BusinessException.WARNING);
					}
				}
			}
			return true;
		}else{
			throw new BusinessException("custservice:custService_event_complete_not_right_exception",BusinessException.WARNING);
		}
	}

	public void resetEStatuzByRevokeSubmitTask(Long taskID, CustServiceEventDO csEvent) {
		if(CustServiceEventDO.EVENT_STATUS_COMPLETE_CODE.equals(csEvent.getEventStatuz())){
			throw new BusinessException("不允许再撤销任务");
		}
		if(CustServiceEventDO.EVENT_STATUS_SUBMIT_CODE.equals(csEvent.getEventStatuz())) {
			Set<SubmitTaskDO> tasks = csEvent.getTasks();
			boolean result = false;
			for(SubmitTaskDO task : tasks){
				if(task.getKeyword()!=taskID && SubmitTaskDO.TASK_VALIDITY_VALID_CODE.equals(task.getValidity())){
					result = true;
					break;
				}
			}
			if(!result){
				csEvent.setEventStatuz(CustServiceEventDO.EVENT_STATUS_NEW_CODE);
			}
		}
	}
}
